#include "../core/engine/engine.h"
#include "sprite.h"

namespace sleek
{
    namespace node
    {
        namespace billboard
        {
            Sprite::Sprite(core::engine::engine *smgr) : Billboard(smgr)
            {
                body = 0;
                current = 0;
                timespec = 100;
            }
            /** ************************* **/
            void Sprite::setTimer(core::u32 i)
            {
                timespec = i;
            }
            void Sprite::setArray(core::math::array<texture::texture*> i)
            {
                anim = i;
                current = 0;
                mat.setTexture(0, i[0]);
                restart();
            }
            /** ************************* **/
            core::math::array<texture::texture*> Sprite::getArray()
            {
                return anim;
            }
            core::u32 Sprite::getTimer()
            {
                return timespec;
            }
            void Sprite::restart()
            {
                current = 0;
            }
            /** ************************* **/
            void Sprite::render()
            {
                if(!anim.size()) return;

                time_stamp.update_begin();
                if(time_stamp.getElapsedTime() > timespec)
                {
                    current += time_stamp.getElapsedTime()/timespec; // sync ( ex: time_stamp = 500, timespec = 100; 500/100 = 5; frame += 5 so frame skipped = 5
                    if(current >= anim.size()) current = 0;
                    mat.setTexture(0,anim[current]);
                    time_stamp.update_end();
                }
                Billboard::render();
            }
        }
    }
}
